import 'dart:math';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:state_manager/initialize_provider.dart';
import 'package:state_manager/model/user_model.dart';
import 'package:state_manager/view_model/counter_view_model.dart';
import 'package:state_manager/view_model/user_info_view_model.dart';

//LocalKey ValueKey、ObjectKey、UniqueKey
//GlobalKey

main() {
  runApp(DBJApp());
}

class DBJApp extends StatelessWidget {
  const DBJApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      theme: ThemeData(primaryColor: Colors.orange, splashColor: Colors.transparent),
      home: DBJHomePage(),
    );
  }
}

class DBJHomePage extends StatefulWidget {
  @override
  _DBJHomePageState createState() => _DBJHomePageState();
}

class _DBJHomePageState extends State<DBJHomePage> {
  GlobalKey<_DBJHomeBodyState> globalKey = GlobalKey();
  @override
  Widget build(BuildContext context) {
    print('_DBJHomePageState 重新build了');
    return Scaffold(
      appBar: AppBar(
        title: Text('打扮家'),
      ),
      body: DBJHomeBody(
        key: globalKey,
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          print('+++++');
          final counter = globalKey.currentState?.counter;
          final name = globalKey.currentState?.widget.name;
          print('counter = $counter, name = $name');
          globalKey.currentState?.test();
        },
        child: Icon(
          Icons.gesture,
          size: 20,
        ),
      ),
    );
  }
}

class DBJHomeBody extends StatefulWidget {
  final String name = 'jose';
  const DBJHomeBody({Key? key}) : super(key: key);

  @override
  _DBJHomeBodyState createState() => _DBJHomeBodyState();
}

class _DBJHomeBodyState extends State<DBJHomeBody> {
  final int counter = 29;
  @override
  Widget build(BuildContext context) {
    return Container();
  }

  void test() {
    print('test --- test');
  }
}
